import { defineStore } from 'pinia';
import { LocalStorage } from 'quasar';
import type { ApiType} from 'src/service/api';
import { mainCategoryList, personalUser } from 'src/service/api';
import { chatNew, chatTag } from 'src/service/api/chat';
import { tokenKey } from 'src/use';

/**
 * Web全局状态
 *
 */

const defultUserIfno = {
  id: '',
  username: '',
  age: -1,
  email: '',
  avatar: '',
  summary: '',
  createTime: '',
  updateTime: '',
  userStatus: 0,
  hasPasswd: false,
  tagList: [],
  artCount: 0,
  favUserNum: 0,
  fansNum: 0,
  favArticleNum: 0,
  readNum: 0,
  homeLink: '',
};

const defultTokenIfno = {
  tokenName: '',
  tokenValue: '',
  isLogin: false,
  loginId: '',
  loginType: '',
  tokenTimeout: 0,
  sessionTimeout: 0,
  tokenSessionTimeout: 0,
  tokenActiveTimeout: 0,
  loginDevice: '',
  powerList: [],
};


export const useWebStore = defineStore('webStore', {
  state: () => {
    return {
      categoryList: [] as ApiType<typeof mainCategoryList>['Return'],
      headReveal: true,
      tokenInfo: { ...defultTokenIfno } as Api.TokenInfoVO,
      userInfo: defultUserIfno as ApiType<typeof personalUser>['Return'],
      searchList: [] as Array<string>,
      loginDig: false,
      chatShowRed: false,
      chatDig: false,
      receiveUser: null as unknown as Api.BaseUserVO,
    }
  },
  getters: {
    // top高度
    topVal(state) {
      if (state.headReveal) {
        return '58px';
      } else {
        return '8px';
      }
    },
    // 是否登录
    isLogin(state) {
      return state.tokenInfo.isLogin;
    },
  },
  actions: {
    async initCategory() {
      const { data, error } = await mainCategoryList();
      if (!error) {
        this.categoryList = data;
      }
    },

    /* 登录成功 */
    async loginSucc(params: Api.TokenInfoVO) {
      // 关闭登录弹窗
      this.loginDig = false;
      // 刷新Token数据
      this.tokenInfo = params;
      // 持久化Token消息
      LocalStorage.set(tokenKey, this.tokenInfo);
      //刷新用户信息
      const { data, error } = await personalUser({
        id: this.tokenInfo.loginId,
      });
      if (!error) {
        this.userInfo = data;
      }
    },
    /* 注销登录 */
    async logout() {
      this.tokenInfo = defultTokenIfno;
      LocalStorage.remove(tokenKey);
      // 用户注销
      this.userInfo = defultUserIfno;
    },
    /* 刷新用户数据 */
    async refreshUserInfo() {
      if (this.isLogin) {
        const { data, error } = await personalUser({
          id: this.tokenInfo.loginId,
        });
        if (!error) {
          this.userInfo = data;
        }
      } else {
        this.userInfo = defultUserIfno;
      }
    },

    // 清理搜索框
    clearSearch() {
      this.searchList = [];
    },
    // 添加搜索框数据
    addSearch(param: string) {
      const index = this.searchList.indexOf(param);
      if (index != -1) {
        this.searchList.splice(index, 1);
      }
      this.searchList.push(param);
    },
    /* 刷新私信小红点 */
    refreshTag() {
      chatTag().then(({ data, error }) => {
        if (!error) {
          this.chatShowRed = data;
        }
      });
    },
    openChatDig(userId: string) {
      chatNew({ userId }).then(({ data, error }) => {
        if (!error) {
          this.receiveUser = data.chatUser;
          this.chatDig = true;
        }
      });
    },
  },
  persist: {
    pick: ['tokenInfo', 'searchList'],
  },
});
